//
//  ScreenSizeManager.cpp
//
//  Created by dino on 13. 7. 9..
//
//

#include "ScreenSizeManager.h"

bool ScreenSizeManager::_isScaleHeight = true;

CCSize ScreenSizeManager::_screenSize = CCSize(0.0f, 0.0f);

CCSize ScreenSizeManager::_imageResolutionSize = CCSize(0.0f, 0.0f);

bool ScreenSizeManager::_isInit = false;

bool ScreenSizeManager::_isFixedScreenSize= false;

void ScreenSizeManager::DoInitFlexibleScreenSize(float myImageResolutionWidth,
                                                 float myImageResolutionHeight)
{
    _isInit = true;
    // cocos2d-x v2.0.4 이상만 지원.
    //CCAssert(0x00020004 <= COCOS2D_VERSION, "ScreenSizeManager need to cocos2d-x 2.0.4 or higher!");
    
    CCDirector* kDirector = CCDirector::sharedDirector();
    
    CCAssert(kDirector->getOpenGLView(), "ScreenSizeManager need to kDirector->getOpenGLView() is not NULL");
    
    CCSize kWinSize = kDirector->getWinSize();
    
    _screenSize.width = kWinSize.width;
    _screenSize.height = kWinSize.height;
    
    _imageResolutionSize.width = myImageResolutionWidth;
    _imageResolutionSize.height = myImageResolutionHeight;
    
    float kDefaultDesignResRatio = _imageResolutionSize.width / _imageResolutionSize.height;
    float kScreenRatio = _screenSize.width / _screenSize.height;
    
    if ( kDefaultDesignResRatio <= kScreenRatio)
    {
        float kHeightRatio = _screenSize.height  / _imageResolutionSize.height;
        // 폭이 넓은 경우
        _imageResolutionSize.width = (int)(_screenSize.width / kHeightRatio);
        _isScaleHeight = true;
    }
    else
    {
        // 높이가 긴 경우
        float kWidthRatio = _screenSize.width / _imageResolutionSize.width;
        // 폭이 넓은 경우
        _imageResolutionSize.height = (int)(_screenSize.height  / kWidthRatio);
        _isScaleHeight = false;
    }
    
    CCLog("DoInitResourceSizeScreen %f %f", _imageResolutionSize.width, _imageResolutionSize.height);
    
    // Cocos2d상의 포인트영역이 _imageResolutionSize.width, _imageResolutionSize.height 으로 설정된다.
    // CCDirector::sharedDirector()->getWinSize()의 width, height 가 _imageResolutionSize.width, _imageResolutionSize.height 로 된다.
    ResolutionPolicy kResolutionPolicy = kResolutionNoBorder;
    CCEGLView::sharedOpenGLView()->setDesignResolutionSize(_imageResolutionSize.width, _imageResolutionSize.height, kResolutionPolicy);
    
}

void ScreenSizeManager::DoInitFixedScreenSize(float myScreenWidth,
                                              float myScreenHeight)
{
    _isInit = true;
    // cocos2d-x v2.0.4 이상만 지원.
    CCAssert(0x00020004 <= COCOS2D_VERSION, "ScreenSizeManager need to cocos2d-x 2.0.4 or higher!");
    
    CCDirector* kDirector = CCDirector::sharedDirector();
    
    CCAssert(kDirector->getOpenGLView(), "ScreenSizeManager need to kDirector->getOpenGLView() is not NULL");
    
    CCSize kWinSize = kDirector->getWinSize();
    
    _screenSize.width = kWinSize.width;
    _screenSize.height = kWinSize.height;
    
    _imageResolutionSize.width = myScreenWidth;
    _imageResolutionSize.height = myScreenHeight;
    
    CCLog("DoInitFixedScreenSizeManager %f %f", _imageResolutionSize.width, _imageResolutionSize.height);
    
    // Cocos2d상의 포인트영역이 _imageResolutionSize.width, _imageResolutionSize.height 으로 설정된다.
    // CCDirector::sharedDirector()->getWinSize()의 width, height 가 _imageResolutionSize.width, _imageResolutionSize.height 로 된다.
    ResolutionPolicy kResolutionPolicy = kResolutionNoBorder;
    CCEGLView::sharedOpenGLView()->setDesignResolutionSize(_imageResolutionSize.width, _imageResolutionSize.height, kResolutionPolicy);
    
}


void ScreenSizeManager::setupFixedScreenSize(bool myFixedScreenSizeManager)
{
    _isFixedScreenSize = myFixedScreenSizeManager;
}

void ScreenSizeManager::initScreenSize(float myScreenWidth,
                                       float myScreenHeight)
{
    if ( _isFixedScreenSize )
    {
        ScreenSizeManager::DoInitFixedScreenSize(myScreenWidth, myScreenHeight);
    }
    else
    {
        ScreenSizeManager::DoInitFlexibleScreenSize(myScreenWidth, myScreenHeight);
    }
    
}
void ScreenSizeManager::changeImageResolutionSize(float myImageResolutionWidth,
                                                  float myImageResolutionHeight)
{
    CCAssert(_isInit, "Need to ScreenSizeManager::init!");
    
    // cocos2d-x v2.0.4 이상만 지원.
    CCAssert(0x00020004 <= COCOS2D_VERSION, "ScreenSizeManager need to cocos2d-x 2.0.4 or higher!");
    
    float kScaleFactor = 1.0f;
    
    if ( _isFixedScreenSize )
    {
        
    }
    else
    {
        if ( _isScaleHeight )
        {
            //
            // 높이를 기준으로 처리...
            int kRemainHeight = (int)myImageResolutionHeight % (int)_imageResolutionSize.height;
            
            if (0 != kRemainHeight)
            {
                float kRatio = kRemainHeight / _imageResolutionSize.height;
                
                if (myImageResolutionHeight < _imageResolutionSize.height)
                {
                    kScaleFactor = kRatio;
                }
                else
                {
                    kScaleFactor += kRatio;
                }
            }
            
            kScaleFactor = myImageResolutionHeight / _imageResolutionSize.height;
            
        }
        else
        {
            // 폭을 기준으로 처리...
            int kRemainWidth = (int)myImageResolutionWidth % (int)_imageResolutionSize.width;
            
            if (0 != kRemainWidth)
            {
                float kRatio = kRemainWidth / _imageResolutionSize.width;
                
                if (myImageResolutionWidth < _imageResolutionSize.width)
                {
                    kScaleFactor = kRatio;
                }
                else
                {
                    kScaleFactor += kRatio;
                }
                
            }
            
            kScaleFactor = myImageResolutionWidth / _imageResolutionSize.width;
        }
    }
    
    CCDirector::sharedDirector()->setContentScaleFactor(kScaleFactor);
}